﻿using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace pandx.Mulan.Identity;

public static class IdentityExtensions
{
    public static IEnumerable<Claim> ReplaceClaim(this IEnumerable<Claim> claimsIdentity, Claim newClaim)
    {
        return claimsIdentity.Select(claim => claim.Type == newClaim.Type ? newClaim : claim);
    }
    public static void ReplaceClaim(this ClaimsIdentity claimsIdentity, Claim newClaim)
    {
        var claim = claimsIdentity.FindFirst(newClaim.Type);
        if (claim != null)
        {
            claimsIdentity.RemoveClaim(claim);
        }

        claimsIdentity.AddClaim(newClaim);
    }
}